<?php


namespace app\business\model;
use think\Db;
use think\Model;
//商品
class Commodity extends Model
{
    /**
     * @findCommodityName   查询商家名
     * @param $businessId
     * @param $commodityId
     * @return Commodity
     */
    public function findCommodityName(int $businessId,int $commodityId){
        return $this->field('commodityName')->where([
            'businessId'        =>  $businessId,
            'id'                =>  $commodityId
        ])->find();
    }

    /**
     * @commodityAllSearch  全部商品搜索
     * @param int $id
     * @return Commodity[]
     */
    public function commodityAllSearch(int $id){
        return $this->field('id,businessId,catId,commodityImg,commodityName,presentPrice,commodityDesc,createTime,isSale,isRecome,deleted')
            ->where([
            'businessId'        =>  $id,
            'deleted'           => 0,
        ])
            ->select();
    }
    /**
     * @commoditySearch 商品搜索
     * @param int $id
     * @param string $commodityName
     * @param int $catId
     * @return Commodity[]
     */
    public function commoditySearch(int $id,string $commodityName){
        return $this->field('id,businessId,catId,commodityImg,commodityName,presentPrice,commodityDesc,createTime,isSale,isRecome,deleted')
            ->where([
            'businessId'        =>  $id,
        ])->whereLike('commodityName',"%$commodityName%")
            ->select();
    }

    /**
     * @showCommodity   显示商品
     * @param int $id
     * @return Commodity[]
     */
    public function showCommodity(int $id){
        return $this->where('businessId',$id)
            ->field('id,commodityImg,commodityName,catId,presentPrice,commodityDesc,createTime,isSale,isRecome,deleted')
            ->where('deleted',0)
            ->select();
    }

    /**
     * @updateCommodity     商品上架或下架
     * @param int $commodityId
     * @param int $businessId
     * @param string $isSale
     * @return Commodity
     */
    public function updateCommodityShelves(int $commodityId,int $businessId,string $isSale){
        return $this->where([
            'id'            =>  $commodityId,
            'businessId'    =>  $businessId
        ])->update([
            'isSale'        =>  $isSale,
            'updateTime'    =>  time()
        ]);
    }

    /**
     * @recommended 商品推荐
     * @param int $commodityId
     * @param int $businessId
     * @param string $recommended
     * @return Commodity
     */
    public function recommended(int $commodityId,int $businessId,string $recommended ){
        return $this->where([
            'id'            =>  $commodityId,
            'businessId'    =>  $businessId
        ])->update([
            'isRecome'      =>  $recommended,
            'updateTime'    =>  time()
        ]);
    }

    /**
     * @delCommodity    商品删除
     * @param int $commodityId
     * @param int $businessId
     * @param string $deleted
     * @return Commodity
     */
    public function delCommodity(int $commodityId,int $businessId,string $deleted ){
        return $this->where([
            'id'            =>  $commodityId,
            'businessId'    =>  $businessId
        ])->update([
            'deleted'       =>  $deleted,
            'updateTime'    =>  time()
        ]);
    }


    //添加商品

    /**
     * @param string $name
     * @param string $filename
     * @param float $price
     * @param string $desc
     * @param int $businessId
     * @param int $typeId
     * @return int|string
     */
        public function  commodityData(string $name,string $filename,float $price,string $desc,int $businessId,int $typeId){
            $CommodityData =[ //商品表
                'businessId' => $businessId,//商家编号
                'catId' => $typeId, //所属分类
                'commodityName' => $name,
                'commodityImg' =>"http://oss-accelerate.aliyuncs.com/hungry/commodity". $filename, //商品图片
                'originalPrice' => $price,
                'presentPrice'=> $price,
                'commodityDesc'=> $desc,
                'createTime'=> time(),
                'updateTime'=> time(),
                'deleted' => 0 //是否删除	做逻辑删除，杜绝物理删除
            ];
            return $this->table('commodity') -> insert($CommodityData);
        }
    public function  findclasscom(int $businessId,int $classid){
        // TODO 请使用model类，不使用助手函数
        $data =$this
            ->table('commodity')
            ->field('id')
            ->where('businessId',"=", $businessId)
            ->where('catId',"=", $classid)
            ->where('deleted','=','0')
//                ->order('id')
            ->select();
        return $data;
    }
    public function updataCommodity(int $commodityId,int $typeId,float $price,string $name ,string $desc)
    {
        $data=[
            'catId' => $typeId, //所属分类
            'commodityName' => $name,
            'presentPrice'=> $price,
            'commodityDesc'=> $desc,
            'updateTime'=> time(),
        ];
        return $this->table('commodity')
            ->where('id',$commodityId)
            ->update( $data );
    }   
    public function updataComimg(int $commodityId,string $filename)
    {
        $data=[
            'commodityImg' =>"http://hungry.wistudy.xyz/". $filename, //商品图片
        ];
        return $this->table('commodity')
            ->where('id',$commodityId)
            ->update( $data );
    }

    //搜索商品
    public function comSearch(int $id,string $commodityName){
        // TODO 请使用model类，不使用助手函数
        return $this
            ->table('commodity')
            ->where('deleted','=','0')
            ->where('businessId','=',$id)
            ->whereLike('commodityName',["%$commodityName%"],'and')
            ->field('id,catId,commodityName,updateTime')
            ->select();
    }
    public function delCom(int $commodityId)
    {
        $data=[
            'updateTime' => time(),
            'deleted' => 1
        ];
        return $this->table('commodity')
            ->where('id',$commodityId)
            ->update( $data );
    }
}